import axios,{type AxiosInstance, type AxiosRequestConfig, type AxiosResponse} from "axios"
import { showNotify } from 'vant';
let instance:AxiosInstance=axios.create({
    baseURL:"/api",
    timeout:5000
})
instance.interceptors.request.use((config:AxiosRequestConfig)=>{
    let token = localStorage.getItem("token")
    if(token){
        (config.headers as any).token = token
    }
    return config
})
instance.interceptors.response.use((res:AxiosResponse)=>{
    if(res.data?.data?.token){
        localStorage.setItem("token",res.data.data.token)
    }
    showNotify({
        message:res.data.message,
        type:res.data.code==="200"?"success":"danger"
    })
    return res.data
})
export default instance